package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class CycleController extends Controller
    {

        public function CycleController()
        {
            
        }

        private function cycleTaskReq() : void
        {
            if (cache.daily.loopBookInfo.needLevel > cache.role.entityInfo.level)
            {
                MsgManager.addBroadCast("等级需要达到" + cache.daily.loopBookInfo.needLevel + "级才可以接循环任务");
                
            }
            if (cache.daily.loopBookInfo.num == cache.daily.loopBookInfo.totalNum)
            {
                MsgManager.addBroadCast("循环任务已全部完成，请明天继续哦");
                
            }
            if (view.isHide)
            {
                view.show();
            }
            else
            {
                GameProxy.cycle.loopTaskReq();
            }
            
        }

        private function onReflashColorReq(event:DataEvent) : void
        {
            var _loc_2:Array = null;
            var _loc_3:QuickBuyWindow = null;
            if (!cache.cycle.cycleInfo.autoBuy && cache.cycle.cycleInfo.freeCount == 0)
            {
                _loc_2 = cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropColorStone);
                if (!_loc_2 || _loc_2.length == 0)
                {
                    _loc_3 = QuickBuyWindow.instance;
                    _loc_3.updateWindow(ECategory._ECategoryProp, EProp._EPropColorStone);
                    _loc_3.show();
                    
                }
            }
            GameProxy.cycle.colorReflashReq(cache.cycle.cycleInfo.autoBuy, cache.cycle.cycleInfo.flushCount, cache.cycle.cycleInfo.flushColor);
            
        }

        private function onTaskGetReq(event:DataEvent) : void
        {
            GameProxy.cycle.taskGetReq();
            
        }

        private function onTaskEndReq(event:DataEvent) : void
        {
            GameProxy.cycle.taskEndReq();
            
        }

        private function onMoveAwardReq(event:DataEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CycleTaskMoveAward, cache.cycle.cycleInfo));
            
        }

        private function onCycleTaskInfoReq(event:DataEvent) : void
        {
            if (_view && !_view.isHide)
            {
                GameProxy.cycle.loopTaskReq();
            }
            
        }

        private function onCycleTaskRes(param1:CycleInfo) : void
        {
            if (!view.isHide)
            {
                (view as ICycleModule).updateCycleInfo(param1);
            }
            
        }

        private function onCycleTaskEnd(param1:ObjCreate) : void
        {
            if (!view.isHide)
            {
                (view as ICycleModule).endCycleCount();
            }
            
        }

        private function onColorFlushRes(param1:CycleInfo) : void
        {
            if (!view.isHide)
            {
                (view as ICycleModule).updateColor(param1);
            }
            
        }

        private function onTaskGetRes(param1:CycleInfo) : void
        {
            if (!view.isHide)
            {
                (view as ICycleModule).getCycle(param1);
            }
            
        }

        private function onTaskUpdateRes(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_4:TaskInfo = null;
            var _loc_5:CycleInfo = null;
            var _loc_3:* = param1.length;
            while (_loc_2 < _loc_3)
            {
                
                _loc_4 = param1[_loc_2];
                if (_loc_4.sTask.group == ETaskGroup._ETaskGroupLoopBook)
                {
                    _loc_5 = cache.cycle.updateTaskProcess(_loc_4);
                    if (_loc_4.status == ETaskStatus._ETaskStatusHadCompleted)
                    {
                        if (view.isHide || _loc_5 == null)
                        {
                            view.show();
                        }
                        else
                        {
                            (view as ICycleModule).updateTaskProcess(_loc_5);
                        }
                    }
                    else if (_view && !_view.isHide && _loc_5 != null)
                    {
                        (view as ICycleModule).updateTaskProcess(_loc_5);
                    }
                    break;
                }
                _loc_2++;
            }
            
        }

        private function onTaskCancelRes(param1:int) : void
        {
            if (_view && !_view.isHide)
            {
                _view.hide();
            }
            
        }

        private function onUseFlushLoopBook(event:DataEvent) : void
        {
            this.cycleTaskReq();
            
        }

        private function onUsePropColorStoneHandler(event:DataEvent) : void
        {
            var _loc_2:* = cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropTaskLoopBook);
            if (_loc_2.length < 0)
            {
                MsgManager.addBroadCast("没有循环任务书");
            }
            this.cycleTaskReq();
            
        }

        private function onTaskListRes(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_4:TaskInfo = null;
            var _loc_5:Array = null;
            var _loc_3:* = param1.length;
            while (_loc_2 < _loc_3)
            {
                
                _loc_4 = param1[_loc_2];
                if (_loc_4.sTask.group == ETaskGroup._ETaskGroupLoopBook && _loc_4.status == ETaskStatus._ETaskStatusHadCompleted)
                {
                    _loc_5 = cache.pack.backPackCache.getTypeItems(ECategory._ECategoryProp, EProp._EPropTaskLoopBook);
                    if (_loc_5.length < 0)
                    {
                    }
                    else
                    {
                        view.show();
                    }
                }
                _loc_2++;
            }
            
        }

        private function onWindowShow(event:WindowEvent) : void
        {
            this.cycleTaskReq();
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new CycleModule();
            _loc_1.addEventListener(WindowEvent.SHOW, this.onWindowShow);
            _loc_1.addEventListener(EventName.CycleReflashColor, this.onReflashColorReq);
            _loc_1.addEventListener(EventName.CycleGetTaskReq, this.onTaskGetReq);
            _loc_1.addEventListener(EventName.CycleEndTaskReq, this.onTaskEndReq);
            _loc_1.addEventListener(EventName.CycleTaskMoveAward, this.onMoveAwardReq);
            return _loc_1;
        }

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskRes, this.onCycleTaskRes);
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskEnd, this.onCycleTaskEnd);
            NetDispatcher.addCmdListener(ServerCommand.CycleColorFlushRes, this.onColorFlushRes);
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskGetRes, this.onTaskGetRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdateRes);
            NetDispatcher.addCmdListener(ServerCommand.CycleTaskCancelRes, this.onTaskCancelRes);
            NetDispatcher.addCmdListener(ServerCommand.TaskListRes, this.onTaskListRes);
            Dispatcher.addEventListener(EventName.UseTaskFlushLoopBook, this.onUseFlushLoopBook);
            Dispatcher.addEventListener(EventName.CycleTaskInfoReq, this.onCycleTaskInfoReq);
            Dispatcher.addEventListener(EventName.UsePropColorStone, this.onUsePropColorStoneHandler);
            
        }

    }
}
